package com.ita.iosc.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.iosc.bean.Merchant;
import com.ita.iosc.service.MerchantManager;
import com.ita.iosc.service.impl.MerchantManagerImpl;

/**
 * Servlet implementation class ListAllMerchantServlet
 */
public class ListAllMerchantServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static MerchantManager merchantManager = new MerchantManagerImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListAllMerchantServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageNum = 1;
		try {			
			pageNum = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			// TODO: handle exception
			pageNum = 1;
		}
		List<Merchant> merchants = merchantManager.listMerchantByPage(pageNum);
		request.setAttribute("merchantList", merchants);
		int count =  (merchantManager.totalMerchant()-1)/12 + 1;
		List<Integer> totalPage = new ArrayList<Integer>();
		for (int i = 0; i < count; i++) {
			totalPage.add(i + 1);
		}
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("currentPage", pageNum);
		request.getRequestDispatcher("./jsp/client/MerchantList.jsp").forward(request, response);
	}

}
